(async () => {
    const [config, lib] = await require("config", "lib");
    const { Stage, Layer, Scene, Sprite, Plate, BehaviorTree } = lib;
    
    const stage = new Stage(document.getElementById("stage"), {
        width: config.display.width,
        height: config.display.height,
        fps: config.display.fps
    });  //创建舞台

    const uiLayer = new Layer("ui-layer");  //UI控件层
    const figureLayer = new Layer("figure-layer");  //角色层
    const mapLayer = new Layer("map-layer");  //地图层
    const backgroundLayer = new Layer("background-layer");  //背景层

    stage.addLayers(uiLayer, figureLayer, mapLayer, backgroundLayer);  //添加图层到舞台

    const scene = new Scene("home", { width: 2000, height: config.display.height });  //创建场景home

    let plate = new Plate({
        x: 0,
        y: 0,
        width: backgroundLayer.width,
        height: backgroundLayer.height,
        url: "background.jpg"
    });  //创建画板
    scene.addChild(plate, backgroundLayer);  //将画板添加到背景图层

    let sprite1 = new Sprite({
        x: 200,
        y: 200,
        width: 110,
        height: 200,
        scale: 1,
        offsetAngle: -90,
        sprite: {
            clipHeight: 600,
            blockWidth: 110,
            blockHeight: 200,
            frameDuration: 200
        },
        url: "NPC_126.png"
    });  //创建精灵1
    scene.addChild(sprite1, figureLayer);  //添加精灵1到角色层

    // let sprite2 = new Sprite({
    //     x: -400,
    //     y: 200,
    //     width: 110,
    //     height: 200,
    //     offsetRotate: -90,
    //     sprite: {
    //         offsetY: 0,
    //         clipHeight: 600,
    //         blockWidth: 110,
    //         blockHeight: 200,
    //         animationSpeed: 1
    //     },
    //     url: "NPC_125.png"
    // });
    // sprite2.animate();  //开始精灵2动画
    // scene.addChild(sprite2, figureLayer);  //添加精灵2到角色层

    setTimeout(() => {
        // sprite1.sprite.offsetY = 600;
        // sprite2.sprite.offsetY = 600;
    }, 2000);

    stage.addChild(scene);  //将场景添加到舞台

    await stage.transitionScene("home");  //转场到场景home

    // sprite1.onUpdate(() => {
    //     sprite1.x += 5;
    // });

    sprite1
    // .setAnimationSpeed(5)
    // .setAnimationTimingFunction("sine.easeInOut")
    .setAnimationLoop(true)
    .animation({ rotate: -45, duration: 1000 })
    .animation({ rotate: 45, duration: 1000 })

    // setTimeout(() => {
    //     sprite1
    //     .interruptAnimation()
    //     .animation({ x: 200, y: 400 })
    // .animation({ rotate: 45 })
    // .animation({ x: 400, y: 600 }, 40)
    // .animation({ rotate: -45 })
    // .animation({ x: 600, y: 400 }, 40)
    // .animation({ rotate: 45 })
    // .animation({ x: 800, y: 600 }, 40)
    // .animation({ rotate: -45 })
    // .animation({ x: 1000, y: 400 }, 10)
    // .animation({ x: 2000, rotate: 0 });
    // }, 2000);

    // sprite1
    // .animation({ x: 200, y: 200 })
    // .animation({ rotate: 45 })
    // .animation({ x: 400, y: 400 })
    // .animation({ rotate: -45 })
    // .animation({ x: 600, y: 200 })
    // .animation({ rotate: 45 })
    // .animation({ x: 800, y: 400 })
    // .animation({ rotate: -45 })
    // .animation({ x: 2000, y: 200, rotate: 0 });
    stage.onKeyDown("up", () => {
        sprite1.y -= 10;
    });
    stage.onKeyDown("left", () => {
        sprite1.x -= 10;
    });
    stage.onKeyDown("right", () => {
        sprite1.x += 10;
    });
    stage.onKeyDown("down", () => {
        sprite1.y += 10;
    });

    /**
     * 渲染调用，何时调用下一次由浏览器决定
     */
    ;(function render() {
        stage.render();  //渲染舞台
        requestAnimationFrame(render);
    })();
})()
.catch(err => console.error(err));